package com.emeraude;

import java.awt.Canvas;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class WorldMap extends Canvas
{
  /**
   * 
   */
  private static final long serialVersionUID  = -264675205440312084L;
  //private final Entity[][] entityMap  = new Entity[10][10];
  public static final int   MAP_HEIGHT        = 1600;
  public static final int   MAP_WIDTH         = 1600;
  public static final int   TILD_WIDTH        = 40;
  public static final int   TILD_HEIGHT       = 40;
  public static final int   NB_TILD_ON_SCREEN = 10;
  private final int[][]     map               = new int[MAP_WIDTH / TILD_WIDTH][MAP_HEIGHT / TILD_WIDTH];
  private final int[][]     mapView           = new int[NB_TILD_ON_SCREEN + 1][NB_TILD_ON_SCREEN + 1];
  private final Game        game;
  private final String      mapPath;

  public WorldMap(final Game game, final String mapPath)
  {
    //map = new int[mapHeight][mapWidth];
    this.game = game;
    this.mapPath = mapPath;
    loadMap(mapPath);
  }

  public void loadMap(final String mapURL)
  {
    final InputStream is1 = getClass().getResourceAsStream(mapURL);
    final BufferedReader buffer = new BufferedReader(new InputStreamReader(is1));

    try
    {
      String mapLine;
      int mapY = 0;
      while ((mapLine = buffer.readLine()) != null)
      {
        final String[] val = mapLine.split(",");
        for (int temps = 0; temps < val.length; temps++)
        {
          map[temps][mapY] = Integer.parseInt(val[temps]);
          //System.out.print(map[temps][mapY] + ", ");
        }
        mapY++;
        System.out.println();
      }
    }
    catch (final IOException e)
    {
      e.printStackTrace();
    }
  }

  public void initMap()
  {

    //loadMap("/maps/level-0-2.map");
    for (int i = 0; i < (MAP_WIDTH / TILD_WIDTH); i++)
    {
      for (int j = 0; j < (MAP_HEIGHT / TILD_HEIGHT); j++)
      {
        Entity mapEntity = null;
        switch (map[i][j])
        {
          case 0:
            mapEntity = new WallEntity(game, "sprites/wall.jpg", i * TILD_WIDTH, j * TILD_HEIGHT);
            game.getEntities().add(mapEntity);
            break;
          case 1:
            mapEntity = new FloorEntity(game, "sprites/floor.jpg", i * TILD_WIDTH, j * TILD_HEIGHT);
            game.getEntities().add(mapEntity);
            break;
          case 2:
            break;
          default:
            break;
        }
      }
    }
  }

  public void initMapView(final int origX, final int origY)
  {
    game.getEntities().clear();
    int paddingX = 2;
    int paddingY = 2;
    if (origX == ((MAP_WIDTH / TILD_WIDTH) - NB_TILD_ON_SCREEN))
    {
      paddingX = 0;
      //origX = (MAP_WIDTH / TILD_WIDTH) - NB_TILD_ON_SCREEN;
    }
    else if (origX == ((MAP_WIDTH / TILD_WIDTH) - NB_TILD_ON_SCREEN - 1))
    {
      paddingX = 1;
    }

    if (origY == ((MAP_HEIGHT / TILD_HEIGHT) - NB_TILD_ON_SCREEN))
    {
      paddingY = 0;
      //origY = (MAP_HEIGHT / TILD_HEIGHT) - NB_TILD_ON_SCREEN;
    }
    else if (origY == ((MAP_HEIGHT / TILD_HEIGHT) - NB_TILD_ON_SCREEN - 1))
    {
      paddingY = 1;
    }
    for (int i = origX; i < origX + NB_TILD_ON_SCREEN + paddingX; i++)
    {
      for (int j = origY; j < origY + NB_TILD_ON_SCREEN + paddingY; j++)
      {
        // System.out.println(map[i][j]);
        Entity mapEntity = null;
        switch (map[i][j])
        {
          case 0:
            mapEntity = new WallEntity(game, "sprites/wall.jpg", i * TILD_WIDTH, j * TILD_HEIGHT);
            game.getEntities().add(mapEntity);
            break;
          case 1:
            mapEntity = new FloorEntity(game, "sprites/floor.jpg", i * TILD_WIDTH, j * TILD_HEIGHT);
            game.getEntities().add(mapEntity);
            break;
          case 2:
            break;
          default:
            break;
        }
      }
    }
  }
}
